package com.gitpractice.liwushuo.utils;

import android.app.Application;

import com.gitpractice.liwushuo.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.io.File;


public class MyApp extends Application {
    private ImageLoader Loader;

    public ImageLoader getLoader() {
        return Loader;
    }
    //先要初始化
    @Override
    public void onCreate() {
        super.onCreate();
        Loader = ImageLoader.getInstance();
        Loader.init(Config());
    }
    //配置ImageLoaderConfiguration这个类实现全局ImageLoader的实现
    private ImageLoaderConfiguration Config() {
        //判断缓存储存路径
        File file = getCacheDir();
        if(!file.exists()){
            file.mkdirs();
        }
        //默认显示图片
        DisplayImageOptions options = defautDisplayOptions();
        //设置配置信息
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPoolSize(3)
                .memoryCacheSizePercentage(30)
                .memoryCache(new WeakMemoryCache())
                .diskCacheSize(5*1024*1024)
                .diskCache(new UnlimitedDiskCache(file))
                .defaultDisplayImageOptions(options)
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .build();
        return config;
    }
    //
    private DisplayImageOptions defautDisplayOptions() {
        return new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .showImageOnLoading(R.mipmap.icon_progress_bar_refresh)
                .showImageOnFail(R.mipmap.placeholder)
                .build();
    }
    //显示的图片的各种格式DisplayImageOptions 的设置
    public DisplayImageOptions imagetRangleOption(){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中
                .showImageOnLoading(R.mipmap.icon_progress_bar_refresh)
                .showImageOnFail(R.mipmap.placeholder)
                .displayer(new RoundedBitmapDisplayer(10))//是否设置为圆角，弧度为多少
                .build();
        return options;
    }
}
